/**
 * Created by Administrator on 2017/4/10.
 */
import sel from '../../index.js'

export default {
  init : function () {
    sel.app.controller('gameControll',['$scope',function ($scope) {
      $scope.title = '俄罗斯方块';
      d3.select('.gameContainer').append('svg');
      var obj = {};
      obj.createGame = function () {
        var rectWidth = 10,padWidth = 160,padHeight = 240;
        var padData = [];
        var movingBlock = null,timer= null,pad = null,blockData,transfromData,num = null;
        var doneBlocks = d3.select('svg').append('g').attr('class','doneBlocks');
        var  colorArr = ["rgb(85,168,253)","rgb(102,204,153)","rgb(255,102,255)","rgb(51,204,102)","rgb(214,194,184)"];
        for(var i = 0 ; i < padHeight/rectWidth ; i++) {
          padData[i] = [];
          for(var j = 0 ; j < padWidth/rectWidth; j++) {
            padData[i][j] = 0;
            var obj = {w:j,h:i};
            doneBlocks.append('rect').attr('class','doneReacts')
            .datum(obj)
            .attr('x',function (d) {
              return d.w*rectWidth
            }).attr('y',function (d) {
              return (d.h+1)*rectWidth
            }).attr('width',0)
            .style('fill','green')
            ;
          }
        };
        d3.select("body")
          .on('keydown',function () {
            if(d3.event.keyCode == 37){
              _game.leftBlock();
            }else if(d3.event.keyCode == 39) {
              _game.rightBlock();
            }else if(d3.event.keyCode == 38) {
              _game.changMovingBlock();
            }
          })
          ;
        function _game() {

        };

        _game.start = function () {
          if(pad && timer) {
            timer.restart(_game.downBlock);
          }else if(pad){
            _game.init();
            _game.createBlock();
          }else {
            _game.createPad();
          }
        };
        _game.init = function () {
          pad.html("");
          pad
          .append('rect')
          .attr('width',padWidth)
          .attr('height',padHeight +rectWidth)
          .attr('stroke','black')
          ;
          d3.select('svg').selectAll('.doneReacts').attr('width',0).attr('height',0);
          padData = [];
          for(var i = 0 ; i < padHeight/rectWidth; i++) {
            padData[i] = [];
            for(var j = 0 ; j < padWidth/rectWidth; j++) {
              padData[i][j] = 0;
            }
          };


        };
        _game.createPad = function () {
          pad = d3.select('svg').append('g').attr('class','game_controller');
          pad
          .append('rect')
          .attr('width',padWidth)
          .attr('height',padHeight + rectWidth)
          .attr('stroke','black')
          ;
          _game.createBlock();
        };
        _game.createBlock = function () {
          _game.changeType();
         transfromData = {x:padWidth/2,y :0};
          //todo 可以进行位置的随机创建
          movingBlock = pad.append('g')
          .attr('class','movingBlock')
          .datum(transfromData)
          .attr('transform',function (d) {
             return ('translate('+ d.x  + ','+ d.y + ')');
          });
          movingBlock
          .selectAll('.movingRect')
          .data(blockData)
          .enter()
          .append('rect')
          .attr('x',function (d) {
            return d.x*rectWidth
          })
          .attr('y',function (d) {
            return d.y*rectWidth
          })
          .attr('width',rectWidth)
          .attr('height',rectWidth)
          .style('fill',function (d,i) {
            return colorArr[i];
          })
          ;
          _game.movingBlock();
        };
        _game.movingBlock = function () {
          timer = d3.timer(_game.downBlock);
        };
        _game.downBlock = function () {
          if(!movingBlock){return};
          var datas = movingBlock.datum();
          var position = {x:datas.x, y:datas.y+1 };
          if(!_game.checkStop(position)){
            _game.movedBlock();
            return
          }
          datas.y += 1;
          movingBlock.attr('transform',function (d) {
            return ('translate('+ d.x  + ','+ d.y + ')');
          });
        };
        _game.movedBlock = function () {
          var pos = transfromData;
          var arr = blockData;
          var paddingX = Math.floor(pos.x /rectWidth);
          var paddingY = Math.floor(pos.y /rectWidth);
          if(paddingY == 0 ){
            console.log('game over');
            timer.stop();
            timer = null;
            return
          }
          for(var i = 0 ; i < arr.length ; i++){
            var x = arr[i].x + paddingX;
            var y = arr[i].y + paddingY;
            padData[y][x] = 1;
          };
          movingBlock.remove();
          movingBlock = null;
          timer.stop();
          timer = null;
          _game.clearBlock();
          _game.createBlock();
        };
        _game.checkStop = function (pos) {
          if(!movingBlock){return};
          var paddingX = Math.floor(pos.x /rectWidth);
          var paddingY = Math.floor(pos.y /rectWidth);
          var arr = blockData;
          for(var  i = 0 ;  i < arr.length ; i++) {
            var x = arr[i].x + paddingX;
            var y = arr[i].y + paddingY;
            if(!padData[y]){
              return false
            }
            if(padData[y][x] == undefined ){
              return false
            }
            if(padData[y][x] == 1){
              return false
            }
          };
          return true;

        };
        _game.pause = function () {
          if(!timer){return};
          timer.stop();
        };
        _game.leftBlock = function () {
          if(!movingBlock){return};
          var datas = movingBlock.datum();
          var position = {x:datas.x -rectWidth, y:datas.y };
          if(!_game.checkStop(position)){
            return
          }
          datas.x -= rectWidth;
          movingBlock.attr('transform',function (d) {
            return ('translate('+ d.x  + ','+ d.y + ')');
          });
        };
        _game.rightBlock = function () {
          if(!movingBlock){return};
          var datas = movingBlock.datum();
          var position = {x:datas.x +rectWidth, y:datas.y };
          if(!_game.checkStop(position)){
            return
          }
          datas.x += rectWidth;
          movingBlock.attr('transform',function (d) {
            return ('translate('+ d.x  + ','+ d.y + ')');
          });
        };
        _game.changeType = function () {
          var blocks = [];
          blocks[0] = [{x:0,y:0},{x:1,y:0},{x:1,y:1},{x:1,y:2}];
          blocks[1] = [{x:0,y:0},{x:1,y:0},{x:2,y:0},{x:0,y:1}];
          blocks[2] = [{x:0,y:0},{x:0,y:1},{x:0,y:2},{x:1,y:2}];
          blocks[3] = [{x:0,y:1},{x:1,y:1},{x:2,y:1},{x:2,y:0}];
          // blocks[0] = [{x:0,y:0,val:1},{x:0,y:1,val:1},{x:0,y:2,val:1},{x:1,y:2,val:1}];
          // blocks[1] = [{x:0,y:0,val:1},{x:1,y:0,val:1},{x:2,y:0,val:1},{x:2,y:1,val:1}];
          // blocks[2] = [{x:0,y:0,val:1},{x:1,y:0,val:1},{x:2,y:0,val:1},{x:1,y:1,val:1}];
          if(num == null ){ num = Math.floor(Math.random()*10) %5};
          blockData = blocks[num];
          num++;
          if(num == 4){
            num = 0
          };

        };
        _game.changMovingBlock = function () {
          if(!movingBlock){return}
          _game.changeType();
          movingBlock.remove();
          movingBlock = pad.append('g')
          .attr('class','movingBlock')
          .datum(transfromData)
          .attr('transform',function (d) {
            return ('translate('+ d.x  + ','+ d.y + ')');
          });
          movingBlock
          .selectAll('.movingRect')
          .data(blockData)
          .enter()
          .append('rect')
          .attr('x',function (d) {
            return d.x*rectWidth
          })
          .attr('y',function (d) {
            return d.y*rectWidth
          })
          .attr('width',rectWidth)
          .attr('height',rectWidth)
          .style('fill',function (d,i) {
            return colorArr[i];
          })
          ;
        };
        _game.updateBlock = function () {
          d3.select('svg').selectAll('.doneReacts').attr('width',function (d) {
            return padData[d.h][d.w] * rectWidth
          }).attr('height',function (d) {
            return padData[d.h][d.w] * rectWidth
          })
        };
        _game.clearBlock = function () {
          var axisY = [];
          for(var i = 0 ; i < padData.length ; i++) {
            var str = padData[i].join('');
            if(/0/ .test(str)){
            }else {
              axisY.push(i);
            }
          };
          if(axisY.length == 0){
            _game.updateBlock();
            return
          }
          for(var j = 0 ; j < axisY.length ; j++) {
            for(var i = axisY[j] ; i > 0 ; i --) {
              padData[i] = padData[i -1];
            };
            _game.updateBlock();
          }


        };
        return _game;
      };

      var gamer = obj.createGame();
      $scope.start = function () {
        gamer.start();
      };
      $scope.pause = function () {
        gamer.pause();
      };


    }])
  }
};